#!/bin/sh

source scripts/common.sh

####################################################################################################

HDF4_VERSION=4.2.5
HDF4_BASENAME="hdf"

HDF4_ARCHIVE="${HDF4_BASENAME}-${HDF4_VERSION}.tar.gz"
HDF4_URL="http://www.hdfgroup.org/ftp/HDF/HDF_Current/src/${HDF4_ARCHIVE}"

HDF4_PATCH_FILE="patch-hdfi.h.diff"
HDF4_PATCH_URL="http://trac.macports.org/export/53005/trunk/dports/science/hdf4/files/${HDF4_PATCH_FILE}"

HDF4_DIR="${HDF4_BASENAME}-${HDF4_VERSION}"
HDF4_PREFIX="${THIRD_PARTY_PREFIX}/${HDF4_BASENAME}"
HDF4_LIB=libdf.a
HDF4_MFLIB=libmfhdf.a

####################################################################################################

function build_HDF4()
{

target=$1

mkdir -p ${THIRD_PARTY_BUILD}
pushd ${THIRD_PARTY_BUILD}

report ""
report "Removing old build from '${HDF4_DIR}'..."
rm -fr $HDF4_DIR
rm -fr $HDF4_PREFIX/${target}

report "Preparing ${target} '${HDF4_BASENAME}'..."
fetch $HDF4_URL $HDF4_ARCHIVE


pushd ${HDF4_DIR} || exit 1
mkdir -p ${target} || exit 1


# report "Patching ${target} '${HDF4_BASENAME}'..."
# report ""

# $FETCH $HDF4_PATCH_URL > $HDF4_PATCH_FILE || exit -1
# patch -p0 < "${HDF4_PATCH_FILE}" || exit -1 

report "Configuring ${target} '${HDF4_BASENAME}'..."
report ""

./configure \
		CXXFLAGS="${SYSROOT} -arch ${target}" \
		CFLAGS="${SYSROOT} -arch ${target}" \
		LDFLAGS="-Wl,-syslibroot,${DEVSDK}" \
		--prefix=${HDF4_PREFIX}/${target} \
        --target=${target}-apple-darwin \
        --disable-shared \
        --enable-static \
        --with-jpeg=${THIRD_PARTY_PREFIX}/jpeg/${target} \
		--with-szlib=no \
		--disable-netcdf \
		--disable-fortran \
		--quiet \
        --silent

if test $? -ne 0; then
        echo "configure failed"
        exit 1
fi

report "Building ${target} '${HDF4_BASENAME}'..."
nice make || exit 1
nice make install || exit 1

report "Built ${target} '${HDF4_BASENAME}'"
report ""

popd
popd

}

####################################################################################################

build_for_snow_leopard
for target in 'i686' 'x86_64' 'ppc'
do
	build_HDF4 $target
done

create_universal_lib ${HDF4_PREFIX} ${HDF4_LIB}
create_universal_lib ${HDF4_PREFIX} ${HDF4_MFLIB}
create_intel_lib ${HDF4_PREFIX} ${HDF4_LIB}
create_intel_lib ${HDF4_PREFIX} ${HDF4_MFLIB}
create_powerpc_lib ${HDF4_PREFIX} ${HDF4_LIB}
create_powerpc_lib ${HDF4_PREFIX} ${HDF4_MFLIB}

####################################################################################################

